package com.ilop.sthome.mvp.present.message;

import android.content.Context;
import android.os.Handler;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.command.SendOtherCommand;
import com.ilop.sthome.data.bean.DeviceHistoryBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.bean.WarnBean;
import com.ilop.sthome.mvp.contract.message.AlarmLogContract;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.example.common.utils.DateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author skygge
 * @Date on 2020-02-22.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class AlarmLogPresenter extends BasePresenterImpl<AlarmLogContract.IView> implements AlarmLogContract.IPresent {

    private Context mContext;
    private List<DeviceHistoryBean> mHistoryList;
    private List<WarnBean> mWarnBeanList;
    private Handler mHandler;

    private int mCurrentPage = 0;
    private boolean isStartSyn = false;

    public AlarmLogPresenter(Context mContext) {
        this.mContext = mContext;
        mHandler = new Handler();
        mWarnBeanList = new ArrayList<>();
        mHistoryList = new ArrayList<>();
    }

    @Override
    public void sendGatewaySyn(int page, boolean isReceive) {
        isStartSyn = true;
        mCurrentPage = page;
        List<DeviceInfoBean> mGateway = DeviceDaoUtil.getInstance().findAllGateway();
        if (mGateway!= null && mGateway.size()>0){
            if (isReceive) {
                mView.showProgress();
            }
            for (DeviceInfoBean gateway : mGateway) {
                SendOtherCommand mSend = new SendOtherCommand(mContext, gateway);
                mSend.syncAlarms(page);
            }
        }else {
            mHandler.post(()->mView.withoutGateway());
        }
    }

    @Override
    public void getAlarmLogs(List<WarnBean> list) {
        if (isStartSyn && mCurrentPage == 1){
            mWarnBeanList = list;
            isStartSyn = false;
        }else {
            mWarnBeanList.addAll(list);
        }
        if (mWarnBeanList.size() > 0) {
            Map<String, List<WarnBean>> map = new HashMap<>();
            for (WarnBean warnBean : mWarnBeanList) {
                String time = DateUtil.getDateToString2(warnBean.getTime()*1000);
                if (map.containsKey(time)) {
                    Objects.requireNonNull(map.get(time)).add(warnBean);
                } else {
                    List<WarnBean> newList = new ArrayList<>();
                    newList.add(warnBean);
                    map.put(time, newList);
                }
            }
            mHistoryList.clear();
            for (Map.Entry<String, List<WarnBean>> entry : map.entrySet()) {
                String dayTime = entry.getKey();
                DeviceHistoryBean device = new DeviceHistoryBean();
                device.setYear(Integer.parseInt(dayTime.split("-")[0]));
                device.setMonth(Integer.parseInt(dayTime.split("-")[1]));
                device.setDay(Integer.parseInt(dayTime.split("-")[2]));
                device.setDaytime(dayTime);
                device.setNumber(entry.getValue().size());
                device.setList(entry.getValue());
                mHistoryList.add(device);
            }
            Collections.sort(mHistoryList);
            Collections.reverse(mHistoryList);
            mHandler.post(() -> mView.showHistory(mHistoryList));
        } else {
            mHandler.post(() -> mView.withoutData());
        }
    }

    @Override
    public void refreshView() {
        List<DeviceInfoBean> mGateway = DeviceDaoUtil.getInstance().findAllGateway();
        if (mGateway== null || mGateway.size()<=0){
            mView.withoutGateway();
        }else {
            if (mHistoryList.size()>0){
                mView.showHistory(mHistoryList);
            }else {
                mView.withoutData();
            }
        }
    }
}
